package com.zusmart.core.condition.extend;

import java.util.Map;

import com.zusmart.core.bean.BeanDefinition;
import com.zusmart.core.condition.Condition;
import com.zusmart.core.condition.ConditionContext;
import com.zusmart.core.config.ConfigSourceManager;
import com.zusmart.core.plugin.Plugin;
import com.zusmart.core.plugin.PluginManager;

public class ConditionalOnHasPluginRule implements Condition {

	@Override
	public boolean matches(ConditionContext conditionContext, Map<String, BeanDefinition> beanDefinitions, BeanDefinition targetBeanDefinition, PluginManager pluginManager, ConfigSourceManager configSourceManager) {
		ConditionalOnHasPlugin hasPlugin = (ConditionalOnHasPlugin) conditionContext.getExtendAnnotation();
		Class<? extends Plugin> clazz = hasPlugin.value().equals(Plugin.class) ? null : hasPlugin.value();
		return this.doMatches(conditionContext, beanDefinitions, targetBeanDefinition, configSourceManager, pluginManager, clazz);
	}

	protected boolean doMatches(ConditionContext conditionContext, Map<String, BeanDefinition> beanDefinitions, BeanDefinition targetBeanDefinition, ConfigSourceManager configSourceManager, PluginManager pluginManager, Class<? extends Plugin> clazz) {
		if (null == clazz) {
			return true;
		}
		return pluginManager.existsPlugin(clazz);
	}

}